<!doctype html>
<html>
	<head>
		<meta charset="utf-8"><meta http-equiv="Expires" content="0"><meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Cache-control" content="no-cache"><meta http-equiv="Cache" content="no-cache">
		<title>协议管理</title>
		<script src="../../../js/vue/vue.js"></script>
		<script src="../../../js/vue/element-ui/lib/index.js"></script>
		<script src="../../../js/vue/page.js?v=20220425"></script>
		<script src="../../../js/myelement.js?v=20220425"></script>
		<!-- <script src="../../../data/data.js?v=20220425"></script> -->
		<script src="../setting.js"></script>
		
		<link href="../../../js/vue/element-ui/lib/theme-chalk/index.css" rel="stylesheet">
		<link href="../../../css/myelement.css?v=20220425" rel="stylesheet">
		<link href="../../../css/iconfont.css" rel="stylesheet">
		<link href="../../../css/page.css?v=20220425" rel="stylesheet">
		<link href="//at.alicdn.com/t/font_2374495_tcs5yi4kbsh.css" rel="stylesheet">
	</head>
	
	<body>
		<div id="vbody">
			<div id="page_root">
				<div class="topbar">
					<span>{{title}}</span>
					
					<div style="float: right; margin-right: 24px;">
						<el-button-group style="margin-left: 3px;">
							<el-button v-if="buttons.printing" :disabled="buttons.printing.disabled" icon="el-icon-bank-card" :loading="isLoading" @click="onAction('print')">协议打印</el-button>
							
							<el-button v-if="buttons.signature" :disabled="buttons.signature.disabled" @click="onAction('signature')">电子签章</el-button>
							<el-button v-if="buttons.receipt" :disabled="buttons.receipt.disabled" @click="onAction('upload')">协议回挂</el-button>
							<!-- <el-upload action="https://jsonplaceholder.typicode.com/posts/" class="z_upload_" style="float: right;">
								<el-button v-if="buttons.signature && selectedrow.field1" size="mini" type="primary" style="display: flex;">协议回挂</el-button>
							</el-upload> -->
						</el-button-group>
						<el-button-group style="margin-left: 3px;">
							<!-- <el-button v-if="buttons.change" :disabled="buttons.change.disabled" @click="onAction('change')">变更申请</el-button> -->
							<!-- <el-button @click="onShowApproval()">审批记录</el-button> -->
						</el-button-group>
					</div>
				</div>
			
				<!-- 查询条件 -->
				<div class="topbar-line">
					<div class="query-icon">
						<i class="iconfont icon-query"></i>
					</div>
					<div class="query-bar">
						<h-form-filter ref="form1" 
							:form-attr="filterAttr" 
							:table-fields="filterFields" 
							:form-data="filterObj" 
							:isbuttonquery="true"
							
							v-on:on-formchange="onQuery" 
							v-on:show-popup="showFilterPopup"
							v-on:on-query="onQuery"
							v-on:on-init-query="onInitFilter"
							v-on:on-edit-query="onEditFilter"
							>
						</h-form-filter>
					</div>
				</div>
			
				<div class="versionNo">
					<h-table
						v-if="isRefresh"
						ref="table1"
						:table-fields="tableFields" 
						:table-data="tableData" 
						:is-edit-table-data="isEditTableData"
						:pagesize="pagesize"
						:pagenum="pagenum"
						:total="total"
						:table-height="tableHeight"
						:is-highlight-row="true"
						v-on:get-data="getData"
						v-on:row-click="rowClick"
						v-on:cell-click="cellClick"
					>
					</h-table>
				</div>
			</div>
			<div id="page_loading" style="position: absolute; top:0px; width: 100vw; height: 100vh;z-index:9999">
				<div class="spinner">
				  <div class="cube1"></div>
				  <div class="cube2"></div>
				</div>
			</div>
		</div>	
		
		<script type="text/javascript">
			var role = Root.role;
			//var setting = getSetting("agreement_list", role.code);
			var initlized = false;
			new ListVue({
				el: "#vbody",
				data: {
					pageAttr: {
						heightType: "page"
					},
					dataname: "protoclostatus",
					tabaleFieldsName: "agreement_",
					filterFieldsName: "agreement_",
					title: "协议管理",
					isLoading: false,
					buttons: [],
					filter_: " and create_operate_code = '" + localStorage.emp_code + "'",
					provinces: [],
					orderType: [],
					orderStatus: [],
					formInline: {},
					begTime: "",
					endTime: "",
					
					isRefresh: true,
					tableFields: [],
					operationtype: "",
					tableData_old: [],
					tableData_new: [],
					tableData2: [],
					tableData: [],
					isEditTableData:false,
					orderby:"update_time desc",
					pagesize: 10,
					pagenum: 1,
					total: 0,
					selectedrow: {},
					pageCode:"A4-4"
				},
				created() {
					/* this.provinces = dataRoot.database.province;
					this.orderStatus = dataRoot.database.orderStatus;
					
					this.tableFields = dataRootFields.tableFields.agreement_;
					this.tableData_new = dataRoot.database.applicationList;
					this.tableData_old = clone(dataRoot.database.applicationList);
					this.tableData2 = this.tableData_new; */
					
					var isdistributor = window.top.isdistributor;
					var distributorObj = window.top.distributorObj;
					
					let roleList = window.top.userinfo.user.roleList;
					let isadmin = false;
					roleList.map(e=>{
						if(e.code == 'admin') {
							isadmin = true;
						}
					})
					if(isadmin) {
						this.filter_ = " and 1=1";
					}
					else if (isdistributor) {
						this.filter_ = " and customer_code = '" + distributorObj.md_code + "'";
					}
					
					this.getResoures();
				},
				
				mounted() {
					this.initData();
					let me = this;
					this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
						hideLoading();
					});
				},
				
				methods:{
					initData() {
						if (initlized) { return; }
						initlized = true;
						
						this.onQuery();
					},
					onServerInitData(data) {
						var me = this.data;
						
						me.tableFields = data.tableFields;
						me.filterFields = data.filterFields;
											
						
						if(me.filterFields.length > 0) {
							for(var i=0; i < me.filterFields.length; i++) {
								let fieldObj_ = me.filterFields[i];
								
								me.filterFieldsObj[fieldObj_.field] = fieldObj_;
							}
						}	
						if(me.tableFields.length > 0) {
							for(var i=0; i < me.tableFields.length; i++) {
								let fieldObj_ = me.tableFields[i];
								
								me.tableFieldsObj[fieldObj_.field] = fieldObj_;
							}
						}
					},
					onCellClick(config, obj) {
						let me = this;
						var clickContext = this.selectCellField.clickContext;
						if(this.selectCellField.field == "code" || (this.selectCellField.field == "signatured_file_name" && obj.row.signatured_file_name)){
							/* if (!obj.row.file_id) {
								Root.message({
									type: 'warning',
									message: '没有附件文件'
								});
								return
							} */
							
							////////////
							let fileid = clone(obj.row.file_id);
							let fileName = clone(obj.row.file_name);
							var row = {
								fileid: fileid,
								filename: fileName,
							}
							let url = "../agreement/agreement/popup_file_pdf_flow.html"
							if(this.selectCellField.field == "signatured_file_name") {
								row.fileid = obj.row.signatured_file_id;
								row.filename = obj.row.signatured_file_name;
								url = "../agreement/agreement/popup_file_pdf_flow.html";
								row.id = obj.row.id;
								row.has_receipt = obj.row.has_receipt
								row.has_showBtn = true;
							}else
								row.has_showBtn = false;
							
							
							
							var config = {
								totab: true,
								icon: "icon-product",
								text: "附件预览",
								id: "pdf_" + fileid,
								url: url,
								data: row,
								delta: me.buttons,
								callback: function(obj, callback) {
									me.doQuery()
									if (callback) {
										callback();
									}
								}
							};
							
							this.doPopupByPublic(config);
							return false;
						}
						
						if (clickContext && clickContext.type == "popup" && clickContext.url != "") {
							config.combine({
								hide_close: clickContext.hide_close,
								url: clickContext.url,
								sceneCode: clickContext.sceneCode,
								data: obj.row,
								delta: clickContext.delta ? clickContext.delta : {},
								width: clickContext.width,
								height:clickContext.height,
							})
						}
						else {
							return false;
						}
						
						return true;
					},
					
					rowClick(obj) {
						this.selectedrow = obj.row;
					},
					onAction(item) {
						let row = this.selectedrow;
						if (!row.id) {
							Root.message({
								type: 'warning',
								message: '请先选择一条数据'
							})
							return;
						}
						if (item == "print") {
							if (!row.file_id) {
								Root.message({
									type: 'warning',
									message: '没有附件文件'
								});
								return
							}
							var url = baseUrl + "file/download?id="+row.file_id;
							dealExportByPath(url, "协议");
							this.selectedrow = {};
						}
						else if (item == "upload") {
							if(this.selectedrow.has_receipt) {
								Root.message({
									type: 'warning',
									message: '已回挂'
								});
								return
							}
							let me = this;
							this.doPopup({
								totab: false,
								width: "1500px",
								icon: "icon-product",
								text: "协议回挂",
								id: "popupByUploadFile",
								url: "../tool/popup_uploadFile.html",
								data: {},
								delta: {filetypelist: [".png", ".jpg", ".pdf"]},
								callback: function(obj, callback) {
									let param = {
										id: me.selectedrow.id,
										isClientMode: false,
										signatured_file_id: obj.row[0].id,
										dataname: "protocol"
									}
									Server.call("root/data/updateEntity", param, function(result) {
										console.log(result);
										if (result && result.data) {
											Root.message({
												type: 'success',
												message: '回挂完成'
											});
											let entity = me.selectedrow;
											let list = [];
											list.push({
													id:uuid_short(),
													type:"protocolHangBack",
													title:"电子签章-" + entity.create_operate_name + "-" + dateFormat(new Date,"yyyy-MM-dd"),
													content:"您的协议-" + entity.code + "- 已回挂",
													fromType:'sys',
													fromUser:"",
													toType:"common",
													toUser:entity.create_operate_id,
													businessId:entity.id,
													wxResult:"",
													ddResult:"",
													readed:false,
													sended:"",
													result:"",
													createTime:dateFormat(new Date,'yyyy-MM-dd hh:mm:ss'),
													flowId:entity.flow_id
												})
											let param_ = {
												list: list
											}
											// debugger
											Server.call("root/api/saveNotification", param_, function(result) {
												if(result.success) {
													/* Root.message({
														type: 'warning',
														message: '发送通知成功!'
													}); */
												}else {
													Root.message({
														type: 'warning',
														message: '发送通知失败!'
													});
												}
											});
											me.selectedrow = {};
											Root.hidePopup();
											me.doQuery();
										}else{
											Root.message({
												type: 'success',
												message: '回挂失败'
											});
										}
										
									});
									if (callback) {
										callback();
									}
								}
							});
							
						}
						else if (item == "signature") {
							this.onseal();
						}
						else if (item == "change") {
							Root.confirm('该协议正在DH2021022801订单收款使用中,请确认是否需要变更吗?', '变更提示', {
							  confirmButtonText: '变更',
							  cancelButtonText: '取消',
							  type: 'warning'
							}).then(() => {
								row.field3 = "已作废";
								this.selectedrow = {};
								Root.message({
									type: 'success',
									message: '变更成功!'
								});
							}).catch(() => {
								Root.message({
									type: 'info',
									message: '已取消变更'
								});          
							});
						}
						this.isRefresh = false;
						this.$nextTick(function(){
							this.isRefresh = true;
						})
					},
					
					onseal(){
						let me = this;
						this.operationtype = "approval";
						
						//2. get config and go
						var config = new Config(), go = true;
						if (this.onApproveData) {
							go = this.onApproveData.call(this, config);
						}
						
						//3. popup
						if (go) {
							
							if (config.totab) {
								if (config.url) {
									if (config.text.length > 4) {
										config.text = config.text.substr(0, 4) + "...";
									}
								}
								var parames = {
									url: config.url,
									sceneCode: config.sceneCode,
									data: config.data,
									delta: config.delta,
									disabled: config.disabled,
									disabledone: config.disabledone,
									callback: function(obj, callback) {
										me.doQuery()
										if (callback) {
											callback();
										}
									},
									operationtype: "approval",
									parentOption: window.top.tab.selected.option
								};
								
								Root.popupParames = parames;
								window.top.tab.open(config);
							}
							else {
								this.doPopup(config);
							}
						}
					},
					onApproveData(config) {
						var me = this;
						var row =  this.selectedrow;
						row.contract_file_id =row.file_id;
						if(row.has_signature) {
							Root.message({
								type: 'warning',
								message: '已签章'
							});  
							return;
						}
						//3. popup
						config.combine({
							
							totab: true,
							icon: "icon-product",
							text: "ç›–ç« ",
							id: "sorder_edit_approval1",
							url: "../order/sorder/signature.html?userid=" + window.top.userinfo.user.id,
							sceneCode: "approval",
							
							data: row,
							delta:{type:"protocol",dataname:"protocol"},
							
						});
						return true;
						
					},
					//表单
					//查询条件中客户弹窗
					popupCustomerList(event) {
						let me = this;
						Root.showPopup({
							 
							url: "../md/customer/popup/customer_list.html",
							width: 800,
							height: 550,
							callback: function(obj, callback) {
								let formInline_ = clone(me.formInline);
								formInline_.field7 = obj.row.field2;
								me.formInline = formInline_;
								if (callback) {
									callback();
								}
							}
						});
					},
					onShowApproval() {
						var me = this;
						var row = this.selectedrow;
						
						if (!row || !row.id) {
							Root.message({
								type: 'warning',
								message: '请先选择一条数据'
							});  
							return false;
						}
						else {
							let param = {
								isClientMode: false,
								flowId:row.flow_id
							}
							
							Server.call("root/order/getHistoryTask/", param, function(result) {
								console.log(result);
								if (result && result.data) {
									
									Root.popupParames = {
										width: "900px",
										height:"700px",
										url: "../approval/ApprovalList.html",
										data: result.data,
									}; 
									// GET /act/historyNode/{flowId}
									// "../approval/ApprovalList.html",
									Root.popupParames.disabled = true;
									Root.showPopup(Root.popupParames);
								}
								
							}); 
							
						}
					},
					
					openMsg3(row) {
						Root.popupParames = {
							width: "900px",
							height:"650px",
							url: "../approval/ApprovalList.html",
						};
						Root.popupParames.disabled = true;
						Root.showPopup(Root.popupParames);
					}, 
					uploadAgreement(code, row) {
						let me = this;
						Root.popupParames = {
							url: "../agreement/tool/upload_agreement.html",
						};
						Root.showPopup(Root.popupParames);
					},
					
					saveRowTable_popup(obj) {
						let formData_ = obj.row;
						let operationtype_ = this.operationtype;
						
						if (operationtype_ == "edit") {//修改
							for (var k in formData_) {
								this.selectedrow[k] = clone(formData_[k]);
							}
						}
						else {//新增
							this.tableData_new.push(formData_);
						}
						this.doQuery();
					},
				}
			});
		</script>
		<style>
			.z_upload_ .el-upload {
				display: flex;
			}
		</style>
	</body>
</html>